package com.lt.bcra.controller.user;

import com.lt.bcra.entity.BcraPersonalInfo;
import com.lt.bcra.entity.vo.BcraPersonalInfoVo;
import com.lt.bcra.exception.BaseException;
import com.lt.bcra.result.ResponseEnum;
import com.lt.bcra.result.ResultData;
import com.lt.bcra.service.BcraPersonalInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * <p>
 * 用户信息登录注册表 前端控制器
 * </p>
 *
 * @author LuoTian
 * @since 2022-05-23
 */
@Api(tags = "个人信息管理")
@RestController
@RequestMapping("bcra/personalInfo")
public class BcraPersonalInfoController {

    private static Logger log = LoggerFactory.getLogger(BcraPersonalInfoController.class);

    @Autowired
    private BcraPersonalInfoService bcraPersonalInfoService;


    @PostMapping("/list/{current}/{size}")
    @ApiOperation("条件分页查询全部用户")
    public ResultData listAll(@PathVariable Long current,
                              @PathVariable Long size,
                              @RequestBody(required = false) BcraPersonalInfoVo bcraPersonalInfoVo) throws BaseException {
        if (log.isDebugEnabled()) log.debug("分页查询全部用户 BcraPersonalInfoController");

        return bcraPersonalInfoService.listByPages(current,size,bcraPersonalInfoVo);
    }

    @GetMapping("/find/{id}")
    @ApiOperation("查询指定用户")
    @ApiParam(value = "用户id", example = "1", required = true)
    public ResultData findById(@PathVariable Integer id) throws BaseException {
        if (log.isDebugEnabled()) log.debug("查询指定用户 BcraPersonalInfoController");
        BcraPersonalInfo bcraPersonalInfo = bcraPersonalInfoService.getById(id);
        return ResultData.getResult(bcraPersonalInfo);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除指定用户")
    @ApiParam(value = "用户id", example = "1", required = true)
    public ResultData deleteById(@PathVariable Integer id) throws BaseException {
        if (log.isDebugEnabled()) log.debug("删除指定用户 BcraPersonalInfoController");
        boolean remove = bcraPersonalInfoService.removeById(id);
        if (remove == false) {
            throw new BaseException(ResponseEnum.DELETE_DATA_ERROR);
        }
        return ResultData.getResult(remove);
    }

    /**
     * 注册用户
     * @param bcraPersonalInfo
     * @return
     * @throws BaseException
     */
    @PostMapping("/creatNew")
    @ApiOperation("创建用户信息")
    @ApiParam(value = "bcraPersonalInfo")
    public ResultData creatNew(@RequestBody BcraPersonalInfo bcraPersonalInfo) throws BaseException {
        if (log.isDebugEnabled()) log.debug("创建用户 BcraPersonalInfoController");

        return bcraPersonalInfoService.registerUser(bcraPersonalInfo);
    }

    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    @ApiParam(value = "bcraPersonalInfo")
    public ResultData update(@RequestBody BcraPersonalInfo bcraPersonalInfo) throws BaseException {
        if (log.isDebugEnabled()) log.debug("修改用户 BcraPersonalInfoController");

        return bcraPersonalInfoService.updateInfo(bcraPersonalInfo);
    }

    @PostMapping("/frozen/{id}")
    @ApiOperation("冻结用户")
    @ApiParam(value = "id")
    public ResultData frozen(@PathVariable Integer id) throws BaseException {
        if (log.isDebugEnabled()) log.debug("冻结用户 BcraPersonalInfoController");

        return bcraPersonalInfoService.frozenUser(id);
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除用户信息")
    @ApiParam(value = "用户id")
    public ResultData deleteBatch(@RequestBody List<Integer> ids) throws BaseException {
        if (log.isDebugEnabled()) log.debug("批量删除用户信息 BcraPersonalInfoController");

        boolean remove = bcraPersonalInfoService.removeByIds(ids);
        if (remove == false) {
            throw new BaseException(ResponseEnum.DELETE_DATA_ERROR);
        }
        return ResultData.getResult(remove);
    }

    @GetMapping("/findPhone")
    @ApiOperation("查询用户手机号")
    public ResultData findAllPhone() throws BaseException {
        if (log.isDebugEnabled()) log.debug("查询用户手机号 BcraPersonalInfoController");
        return bcraPersonalInfoService.getAllPhone();
    }

    @GetMapping("/login/{phone}/{password}")
    @ApiOperation("用户登录,密码登录")
    public ResultData login(@PathVariable String phone,@PathVariable String password) throws BaseException {
        if (log.isDebugEnabled()) log.debug("用户登录 BcraPersonalInfoController");
        return bcraPersonalInfoService.loginUser(phone,password);
    }

    @GetMapping("/loginBySms/{phone}")
    @ApiOperation("用户登录,验证码登录")
    public ResultData loginBySms(@PathVariable String phone) throws BaseException {
        if (log.isDebugEnabled()) log.debug("验证码手机验证码登录 BcraPersonalInfoController");
        return bcraPersonalInfoService.loginBySms(phone);
    }

    @ApiOperation("用户信息导出")
    @PostMapping(value = "/exportExcel")
    public void exportExcel(@RequestBody BcraPersonalInfoVo bcraPersonalInfoVo, HttpServletResponse response) throws BaseException{
        if(log.isDebugEnabled()) log.debug("用户信息导出 BcraPersonalInfoController");
        bcraPersonalInfoService.exportExcel(response,bcraPersonalInfoVo);
    }

    @ApiOperation("令牌校验")
    @GetMapping(value = "/checkToken")
    public ResultData checkToken(HttpServletRequest request){
        if (log.isDebugEnabled()) log.debug("令牌校验 BcraPersonalInfoController");

        return bcraPersonalInfoService.checkToken(request);
    }
}

